//
//  HZPeiziWeiTuoVC.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/29.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziWeiTuoVC.h"
#import "HZPeiziQueryView.h"
#import "MYHttp.h"

@interface HZPeiziWeiTuoVC ()
@property (nonatomic, strong)HZPeiziQueryView *pQueryView;
@property (nonatomic, strong)NSMutableArray *pQueryArray;
@property (nonatomic, strong)NSString *pStartDate;
@property (nonatomic, strong)NSString *pEndDate;
@property (nonatomic, strong)NSString *pShowType;
@end

@implementation HZPeiziWeiTuoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"委托查询";
    
    _pQueryArray = [NSMutableArray new];
    
    //关联逻辑管理对象
    self.logicManager = [HZBaseLogjcManager new];
    self.view.logicManager = self.logicManager;
    
    _pQueryView = [HZPeiziQueryView new];
    _pQueryView.nQueryType = kQuery_entrust_Type;
    [self.view addSubview:_pQueryView];
    [_pQueryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [self p_registerBlock];
}

#pragma mark -
#pragma mark blockAction
- (void)p_registerBlock
{
    __weak typeof (self)weakSelf = self;
    [self.logicManager mapLogic:kLogicRouterPeiziWeiTuoFind toBlock:^id(NSDictionary *params, id other) {
        if([other isKindOfClass:[NSArray class]]){
            NSArray *pTempArray = (NSArray *)other;
            if(pTempArray.count == 3){
                _pStartDate = pTempArray[0];
                _pEndDate = pTempArray[1];
                _pShowType = pTempArray[2];
                
                if([_pShowType isEqualToString:@""]){
                    [weakSelf p_sendExcutingRequestWithStartDate:_pStartDate endDate:_pEndDate showType:_pShowType];
                }else if([_pShowType isEqualToString:@"1"]){
                    [weakSelf filterQueryData:@"买入"];
                }else if([_pShowType isEqualToString:@"2"]){
                    [weakSelf filterQueryData:@"卖出"];
                }
            }
        }
        return nil;
    }];
    [self.logicManager mapLogic:kLogicRouterExcute_clickCancel toBlock:^id(NSDictionary *params, id other) {
        [weakSelf clickCancelBtn:other];
        return nil;
    }];
}

- (void)filterQueryData:(NSString *)showType{
    if(_pQueryArray.count > 0){
        NSMutableArray *tempArray = [NSMutableArray array];
        for(int i=0;i<_pQueryArray.count;i++){
            HZPeiziCancelOrderListModel *model = (HZPeiziCancelOrderListModel *)_pQueryArray[i];
            if([model.bstype isEqualToString:showType]){
                [tempArray addObject:model];
            }else if([model.bstype isEqualToString:showType]){
                [tempArray addObject:model];
            }
        }
        [_pQueryView updateNewData:tempArray];
    }
}

- (void)clickCancelBtn:(HZPeiziCancelOrderListModel *)model
{
    [SVProgressHUD show];
    
    NSArray *array = @[@{@"funcid":@"11", @"orderno":model.sno,@"session":[HZUserManager manager].peiziAccountSession}];
    [MYHttp post:kAPIPeiziHost params:array success:^(id response) {
        [SVProgressHUD showSuccessWithStatus:@"撤单委托提交成功"];
        if([_pShowType isEqualToString:@""]){
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self p_sendExcutingRequestWithStartDate:_pStartDate endDate:_pEndDate showType:_pShowType];
            });
        }else{
            for(int i=0;i<_pQueryArray.count;i++){
                HZPeiziCancelOrderListModel *aModel = (HZPeiziCancelOrderListModel *)_pQueryArray[i];
                if([aModel.sno isEqualToString:model.sno]){
                    [_pQueryArray removeObject:aModel];
                }
                if([_pShowType isEqualToString:@"1"]){
                    [self filterQueryData:@"买入"];
                }else if([_pShowType isEqualToString:@"2"]){
                    [self filterQueryData:@"卖出"];
                }
            }
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self sendRequest];
}

- (void)sendRequest
{
    _pStartDate = @"";
    _pEndDate = @"";
    _pShowType = @"0";
    [self p_sendExcutingRequestWithStartDate:_pStartDate endDate:_pEndDate showType:_pShowType];
}
#pragma mark -
#pragma mark request
- (void)p_sendExcutingRequestWithStartDate:(NSString *)startDate
                                 endDate:(NSString *)endDate
                                showType:(NSString *)showType
{
    [SVProgressHUD show];
    NSArray *array = @[@{@"funcid":@"2",@"startdate":startDate,@"enddate":endDate,@"showtype":showType,@"session":[HZUserManager manager].peiziAccountSession}];
    [MYHttp post:kAPIPeiziHost params:array success:^(id response) {
        [_pQueryArray removeAllObjects];
//        NSMutableArray *tempArray = [NSMutableArray array];
        if([response isKindOfClass:[NSArray class]]){
            for (NSDictionary *dict in response) {
                HZPeiziCancelOrderListModel *model = [HZPeiziCancelOrderListModel mj_objectWithKeyValues:dict];
                [_pQueryArray addObject:model];
            }
        }
//        if(_pQueryArray.count > 0){
          [_pQueryView updateNewData:_pQueryArray];
//        }
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}

- (void)dealloc{
    DLog(@"11111")
}

@end
